package com.sb.registration.component.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import com.sb.registration.component.model.UserWrapper;
import com.sb.registration.component.service.UserValidator;
import com.sb.registration.component.util.UserValidationUtil;

/**
 * Validation Class for validating registration user data.
 * 
 * @author gautam.amitkumar@gmail.com
 *
 */
@Component
public class UserValidatorImpl implements UserValidator {	
	
	public UserValidatorImpl(){
		
	}
	
	/**
	 * This method validates user data provided for registration. For simplification, all fields will be validated 
	 * in the same request and a common string message will be generated for all validation errors separated by commmas.
	 * 
	 * in case of no validation errors, a blank string will be returned from this method. 
	 * 
	 * NOTE- THIS IMPLEMENTATION IS DONE ONLY TO REDUCE THE EFFORT. THIS CAN BE DONE IN A BETTER WAY BY DEFINE VALIDATION RESULT OBJECT HAVING
	 * LIST OF VALIDATION ERRORS AND RESPONSE ACCORDINGLY
	 * 
	 * @param userData
	 * @return
	 */
	public String validateUser(UserWrapper userData){
		
		StringBuffer sb = new StringBuffer();
		
		//VALIDATE EMAIL
		if(StringUtils.isNotEmpty(userData.getEmail())){
			if(!(UserValidationUtil.isValidEmail(userData.getEmail()) && UserValidationUtil.isValidEmailDomain(userData.getEmail()))){
				sb.append("Invalid Email Id Provided - ").append(userData.getEmail()).append(" <br/>");
			}
		}else{
			sb.append("Email Id Not Provided <br/>");
		}
		
		
		// VALIDATE CHANNEL
		
		if(StringUtils.isNotEmpty(userData.getChannel())){
			if(!UserValidationUtil.isValidChannel(userData.getChannel())){
				sb.append("Invalid Channel Provided - ").append(userData.getChannel()).append(" <br/>");
			}
		}else{
			sb.append("Channel Not Provided <br/>");
		}
		
		// VALIDATE IP ADDRESS
		if(StringUtils.isNotEmpty(userData.getIpaddress())){
			if(!UserValidationUtil.validateIPAddress(userData.getIpaddress())){
				sb.append("Invalid IP Address Provided - ").append(userData.getIpaddress()).append(" <br/>");
			}
		}else if(userData.getChannel().equalsIgnoreCase("Web") || userData.getChannel().equalsIgnoreCase("Mobile")){
			sb.append("IP Address Not Provided <br/>");
		}
		
		
		return sb.toString();
		
	}

}
